<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: TranslateVar2.php 262 2011-07-23 15:51:14Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//class Permission extends Baogg_Db_Table {
class Content  extends Baogg_Db_Table {
	/**
	 * The default table name
	 */
	protected $_name;
    protected $_db ;
    
	function __construct() {
		$this->_name =   'translate_var';
		$this->primary= $this->_primary='translate_var_id';
		parent::__construct();
	}

	function getList() {
		global $db,$config;
		$session_prefix = getPageName();

		if (isset($_GET['module_id'])){
			$_SESSION["s_admin_{$session_prefix}_multilang_module_id"] = trim($_REQUEST['module_id']);
			cleanCurragePage($session_prefix);
		}
		$s_module_id = $_SESSION["s_admin_{$session_prefix}_multilang_module_id"];

		if ($s_module_id){
			if($s_module_id!="all")	{
				//if (strlen($s_module_id)==3)
					$condition .= " and mp.module_permission_id in (select module_permission_id from module_permission where module_permission_pid ='{$s_module_id}' or module_permission_id ='{$s_module_id}') ";
				//else
					//$condition .= " and mp.module_permission_id='{$s_module_id}' ";
			}
		}

		if (isset($_REQUEST['search']))	{
			$_SESSION["s_admin_{$session_prefix}_language_key"] = trim($_REQUEST['search']);
		}
		$s_keyword = $_SESSION["s_admin_{$session_prefix}_language_key"];

		if($s_keyword)	{
			$condition .= " and ( mc.variable_name like '%{$s_keyword}%' or mt.content like '%{$s_keyword}%') ";
		}

		//order by
		$orderby = " order by ";
		if ($_GET['dir']=="ASC" || $_GET['dir']=="DESC" ){
			if ($_GET['sort']){
				$orderby .= $_GET['sort'] ." ".$_GET['dir'].",";
				switch ($_GET['sort']) {
					case "variable_name":
					   $orderId=1;
					   break;
					case "permission_name":
					   $orderId=2;
					   break;
					case "content":
					   $orderId=3;
					   break;
					case "content_type":
					   $orderId=4;
					   break;
					case "updated_time":
					   $orderId=5;
					   break;
				}

				$_SESSION[$session_prefix."_orderby"]=$orderId;
				$_SESSION[$session_prefix."_orderby_desc"]=$_GET['dir'];
			}
		}
		$orderby.= " updated_time desc  ";

		$sql_count = "select count(*) as total
				from multilang_content mc, module_permission mp,
					language l,multilang_translate mt ,
					multilang_table mut ,multilang_info mui
				where  mc.module_permission_id=mp.module_permission_id
				and mui.multilang_table_id=mut.multilang_table_id
				and mp.module_permission_id=mui.key_id
				and l.language_id=mui.language_id
				and mui.language_id=1
				and l.language_id=mt.language_id
				and mut.table_name='module_permission'
				and mt.multilang_content_id=mc.multilang_content_id
				$condition ";
		//echo $sql_count;
		$stmt='';     $stmt=$db->query($sql_count);
		$rs_count = $stmt->fetchAll();
		//print_r($rs_count);
		$rows = $rs_count[0]["total"];

		$pager = getPagerParam($session_prefix);

		$sql = "select
					mt.multilang_content_id as multilang_content_id,
					mp.module_permission_id as module_permission_id,
					mui.content as permission_name,
					mc.variable_name as variable_name,
					mc.content_type as content_type,
					mp.module_permission_code as module_permission_code,
					mt.content as content,
					mt.updated_time as updated_time,
					mc.updated_time as created_time
				from multilang_content mc, module_permission mp,
						language l,multilang_translate mt ,
						multilang_table mut ,multilang_info mui
				where  mc.module_permission_id=mp.module_permission_id
					and mp.module_permission_id=mui.key_id
					and mui.multilang_table_id=mut.multilang_table_id
					and l.language_id=mui.language_id
					and mui.language_id=1
					and l.language_id=mt.language_id
					and mut.table_name='module_permission'
					and mt.multilang_content_id=mc.multilang_content_id
				 $condition  $orderby limit $pager->_offsize,$pager->_limit ";
		//echo $sql;

		$stmt='';     $stmt=$db->query($sql);
		$rs = $stmt->fetchAll();

		if(is_array($rs)){
			foreach ((array)$rs as $key => $value) {
				$rs[$key]['action'] =editActionLink("{$config['web']['baseurl']}multilang/content/edit?multilang_content_id={$value["multilang_content_id"]}&module_permission_id_post={$value["module_permission_id"]}");//"<a href='javascript:showDialog(\"Edit\",\"content_edit.php?task=edit&multilang_content_id={$value["multilang_content_id"]}&module_permission_id_post={$value["module_permission_id"]}\")'><img src='../images/icons/action/edit_link.gif''></a>";
				//$rs[$key]['content_type']="title";
				$rs[$key]['content_type']=$config['global']['g_multilang_contenttype'][$rs[$key]['content_type']];
				$rs[$key]['updated_time']=showTime($rs[$key]['updated_time'],1);
				$rs[$key]['created_time']=showTime($rs[$key]['created_time'],1);
			}
		}else{
			$rs=array();
		}

		$listData=array(
			"total"=>$rows,
			"rs"=>$rs
		);
		return $listData;
	}




	function _update($arr, $id) {
		$where = $this->db->quoteInto('language_id = ?', $id);
		$rows_affected = $this->db->update($this->_name, $arr, $where);
		return $rows_affected;
	}

	function _insert($arr) {

		$rows_affected = $this->db->insert($this->_name, $arr);
		$last_insert_id = $this->db->lastInsertId();

		return $rows_affected;
	}

	function _delete($id) {
		$sql_pre="DELETE multilang_content,multilang_translate " .
			" from multilang_translate LEFT JOIN multilang_content ".
			" ON multilang_content.multilang_content_id=multilang_translate.multilang_content_id  ".
			" WHERE multilang_translate.multilang_content_id=";
		$rows_affected=$this->db->query($sql_pre.$id);

		return $rows_affected;
	}


	function getOne($id) {
		$sql = $this->db->quoteInto('SELECT * FROM ' . $this->_name . ' WHERE multilang_content_id = ? ', $id);
		$rs = $this->db->query($sql)->fetchAll();
		return $rs;

	}

	function getById($id,$orderby="multilang_content_id",$orderby_desc="ASC") {

		if (is_array($id)) {
			//if $id is array then implode to sql's where in statement
			$content_ids=implode(',',$id);
			$condition = " and multilang_content.multilang_content_id in ({$content_ids})";
		}elseif((int)$id != '') {
			$condition = " and  multilang_content.multilang_content_id='".$id."'";
		}else{
			$condition = " and multilang_content.multilang_content_id in ()";
		}


		if ($orderby_desc=="DESC") {
			$order_desc=" desc ";
		}


		$sql = "select * from multilang_content, multilang_translate
			where  		1=multilang_translate.language_id
			and multilang_translate.multilang_content_id=multilang_content.multilang_content_id
			 $condition ";
		//echo $sql;

		$sql = $this->db->quoteInto($sql, $id);
		$contentData = $this->db->query($sql)->fetchAll();

		return $contentData;

	}



}
